Skip to content

Reimplement 2 TileMapState functions that need a local noinline forwarder or a volatile read in a local #define - #560

Open
gynt wants to merge 8 commits into
sourcehold:mainfrom
gynt:reimpl/tilemapstate-match-tricks
Open

gynt wants to merge 8 commits into
sourcehold:mainfrom
gynt:reimpl/tilemapstate-match-tricks

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

These two Map::TileMapState functions come from #396. They only match through tricks the review objects to, so they get their own PR, as #519 and #559 did for earlier functions.

The tricks

getHeightAtTileIncludingOwnersBuildings (0x004FFF10): local noinline forwarder, 99.3% raw (100% normalized).

  • Both the tower arm and the gatehouse arm end in return getTotalHeightAtTile(tile);. In the original they are two separate calls. The two targets are probably byte-identical functions that the linker folded into one address; only one resolver exists for 0x004FFB10.
  • If both arms call the function directly, MSVC merges the identical tails, and the second arm jumps into the first.
  • The .cpp therefore declares a __declspec(noinline) member forwarder in an anonymous namespace and calls it from the gatehouse arm. The code matches; reccmp reports only the forwarder's name at that call.

updateGfxLayer (0x00509180): GMFirstPictureIndex::instance[9] read through a volatile cast in a local #define, 100% raw (95.2% normalized).

  • #define GM_FIRST_PICTURE_INDEX(I) (*(int volatile*)&GMFirstPictureIndex::instance[I]) covers the five PillarGFXLayer stores.
  • Without the volatile, the compiler merges those stores and the blocks around them. The original keeps them separate.

Trick-free forms tried

Each was measured with a full build and reccmp, both resolver sets on, every TileMapState source on main plus these two in the build:

Function Form Raw
getHeightAtTileIncludingOwnersBuildings with the forwarder (this PR) 99.3
both arms call getTotalHeightAtTile through the resolver directly 89.3 (the tails merge)
gatehouse arm calls it on &DAT_TileMapState::instance 63.9
updateGfxLayer with the volatile #define (this PR) 100.0
plain GMFirstPictureIndex::instance[9] 72.6
an int const pillarPicture local per store 77.4

No existing enum or constant on main stands in for index 9, and the only other resolver for 0x004FFB10 is the one the direct call already uses. A form without either trick would need a second, header-declared function for the folded twin of getTotalHeightAtTile, which is a generated-header change.

Review pass

Compared with #396:

  • In updateGfxLayer, the other local macros are expanded inline: DIRECTION_OFFSET, SEA_EDGE_PICK, ROTATE_BITFLAG_BY_ORIENTATION and MIN_OF (now Windows' min). Only the volatile #define is left.
  • In getHeightAtTileIncludingOwnersBuildings, the == 0/!= 0 tests are now truthiness checks.
  • In updateGfxLayer, the numbers are named: directions use CD_*/CD_UPPER_LIMIT, the shore switch cases use NM_* combinations, wall corners use the new WallCornerType enum (OpenSHC/Map/Enums/WallCornerType.hpp, the only header this PR adds), and the TerrainFlagGraphicLookup scans use countof - 1.

Two forms are kept because the readable one costs match:

  • this->directionTranslationMatrix[0][this->DAT_SomeY * CD_UPPER_LIMIT + d] keeps its flat form. [this->DAT_SomeY][d] changes the generated code: 93.4% raw / 49.0% normalized.
  • local_14_f keeps its name. The original's stack layout puts it in its declared slot only under this name. Each of the 20 other names tried (lookupIndex, i, fordIndex, flagIndex, local_14_g, …) moves it to the top of the frame in the compiler's listing and shifts three other locals. With lookupIndex the function drops from 100 to 98.8%.

No /GL or function-visibility entries are needed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 3 commits December 4, 2026 19:04
… local noinline forwarder)

From sourcehold#396. The gatehouse arm calls getTotalHeightAtTile through a __declspec(noinline) forwarder local to the .cpp, so the compiler does not merge it with the tower arm's identical call. reccmp reports only the forwarder's name at that call (100% normalized).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
… a volatile cast)

From sourcehold#396. The five PillarGFXLayer stores read GMFirstPictureIndex::instance[9] through a local GM_FIRST_PICTURE_INDEX #define that casts to int volatile, which keeps the compiler from merging them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…atch-tricks

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Match regression check

1 of the 2 status entries behind the files this PR changes are below the best match already known on HEAD or on a fork's branch:

file id here best known where known remark
src/OpenSHC/Map/TileMapState/getHeightAtTileIncludingOwnersBuildings.cpp SHC_3BB0A8C1_0x004FFF10 99.3% (Only the call target differs: getTotalHeightAtTile is called through a local noinline forwarder) 100.0% Wilps93-OpenSHC/reimpl/TileMapState-100-2 Reimplemented

Percentages are read from the status/addresses-SHC-*.txt files as written; nothing is rebuilt here. If a figure above is stale, update the status file.

…atch-tricks

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt

gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

updateGfxLayer keeps its 7 gotos. No goto-free form I tried keeps the match. The goto form measures 100.0% raw / 95.2% normalized, from a full build plus reccmp with both resolver sets on, on main 55fa231 merged in.

The gotos jump to two shared blocks at the end of the last else if arm: wallAlpha and plainTerrain.

  • wallAlpha is reached from the pitch-ditch arm, the moat arm and the last arm. plainTerrain is reached from the pitch-ditch arm, the building arm and the end of wallAlpha.
  • The wallAlpha block contains the inline-asm MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8. MSVC does not tail-merge code around inline asm, so the duplicated copies stay separate.

Forms tried (raw / normalized):

  • Duplicated tails: a copy of wallAlpha and plainTerrain at every goto, with the arms restructured into if/else (and a distinct tag for each copy of the inline-asm macro). 79.7 / 28.6. The copies are not folded back together: our function grows from 7717 to 9970 listing lines.
  • Two helper flags (BOOLEnum useWallAlpha, usePlainTerrain): the arms set them, and the two blocks run once after the chain, before the wall/keep pillar check. Behaviour is the same. 77.4 / 72.3. The flag stores and tests change the block layout throughout.
  • One flag for wallAlpha, plainTerrain duplicated: 65.1 / 57.9.

Flag forms, tried systematically. 78 variants generated from the goto form and compiled one by one. Each listing was compared against the 100% goto form's own listing, which is more precise than quick_diff's ratio for this function, since quick_diff misreads the original's length here. The space covered:

  • flag type: BOOLEnum, BOOL, int, bool, char (and short for the tri-state);
  • shape: one flag per label, a tri-state terrainPath 0/1/2, or the inverted sense (skipWallAlpha starting at TRUE);
  • declaration: initialised just before the if/else if chain, or declared first or last among the function's locals and assigned before the chain;
  • blocks after the chain, either in sequence (if (wallAlpha) {…} if (plainTerrain) {…}, with wallAlpha setting plainTerrain where it fell through) or nested under if (wallAlpha || plainTerrain).

Findings:

  • The best family is an int-sized flag (or the tri-state) declared and initialised just before the chain, with the blocks in sequence: 77.4 / 72.3 in reccmp for both the two-flag and the tri-state form.
  • Declaring the flags among the top locals drops it to 69.2 / 63.0. bool/char flags are worse still in the listing comparison.
  • Every flag form needs its own stack slot: the frame grows from sub esp, 280 to 288, because locals cannot live in registers in a function with inline asm. It also needs a store on each arm and a test after the chain, none of which exist in the original. There, each arm jumps straight into the shared block.
  • Keeping the last arm's own blocks in place while the other arms set flags would mean a second copy of the inline-asm wallAlpha block (see the duplicated forms above).

So no flag form can reach 100%, and the gotos stay.

Casts kept for the match (cast pass: each cast removed one at a time and the listing compared; none was redundant):

  • updateGfxLayer: WallGFXLayer[...] = (byte)wallHeightIndex. Without it, the listing changes. Declaring wallHeightIndex as byte also changes the code (53 listing lines).
  • updateGfxLayer: MiscDisplayLayer[...] & (ushort)~MD_PILLAR_SIDESUnk. MiscDisplayLayer is declared short, so without the cast the mask is emitted as and eax, -4 instead of the original's and eax, 0xfffc. A ushort layer type would make the cast unnecessary, but the layer has many users on main, so that is a Ghidra-side follow-up.
  • getHeightAtTileIncludingOwnersBuildings: the static_cast to the forwarder is the documented trick and is left as is.

…atch-tricks

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
switch (cornerMain) {
case 1:
firstTile3 = this->DAT_SomeTile
+ this->directionTranslationMatrix[0][this->DAT_SomeY * 8 + 2];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this indicate another array dimension, or would this not make sense with the other uses?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: directionTranslationMatrix is already int [400][8]. But [this->DAT_SomeY][CD_EAST] everywhere drops updateGfxLayer from 100.0/95.2 to 93.4/49.0, so the flat [0][DAT_SomeY * CD_UPPER_LIMIT + CD_EAST] stays. The 8 and the offsets now have names (6f13203).

}
}
if (cornerMain != 0) {
switch (cornerMain) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was that actually? Are its values numeric or enum like?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum-like: it is the corner a wall tile forms with its two cardinal wall neighbours (1 = E+S, 2 = S+W, 3 = N+W, 4 = N+E). I added WallCornerType (WCT_*, b96a50b) and use it for every assignment and case (6f13203). The listing is identical.

tileXTmp = this->DAT_SomeX;
if ((this->LogicLayer[this->DAT_SomeTile] & L_SEA) != 0) {
this->MiscDisplayLayer[this->DAT_SomeTile]
= this->MiscDisplayLayer[this->DAT_SomeTile] & 0xfffc;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flag known?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0xfffc clears bits 0-1, which is MD_PILLAR_SIDESUnk. It is now & (ushort)~MD_PILLAR_SIDESUnk (6f13203). The cast stays because MiscDisplayLayer is declared short: without it, the constant becomes and eax, -4 instead of the original and eax, 0xfffc.

}
shallowMaskMain = 0;
this->bitFlag = 0;
for (directionTmp = 0; directionTmp < 8; directionTmp++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is the 8 directions. The loop is now < CD_UPPER_LIMIT, and so are the other bit and direction loops (pathBitValue, edgeBitId, moatDirectionValue). The field_0x370c scan uses countof(...) (6f13203).

if ((this->LogicLayer[neighbourTilePos]
& (L_SEA | L_BORDER | L_BORDER_EDGE | L_RIVER))
== 0) {
this->bitFlag |= 0x80 >> directionTmp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flag known?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: 0x80 is NM_NORTH, and NM_NORTH >> d is the NeighbourMask bit for CardinalDirection d (N=0x80, NE=0x40, ... NW=1). It is now written that way at all six places (6f13203).

if (this->bitFlag != 0) {
if ((this->bitFlag & NM_NORTH) != 0) {
if ((this->bitFlag & NM_EAST) != 0) {
bankDirection = 5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the enum directions, or something else?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are directions: the CardinalDirection the bank faces, away from the water (water N gives 4 = CD_SOUTH, water N+E gives 5 = CD_SOUTHWEST). They are rotated by mapOrientation in the same CD space. All assignments now use CD_* (6f13203).

MACRO_NEIGHBOUR_FLAGS_GLOBAL_LOGIC_8(riv7, L_RIVER)
if (this->bitFlag != 0) {
tileHeightAt = this->HeightLayer[this->DAT_SomeTile];
for (local_14_f = 0; local_14_f < 8; local_14_f++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the size of field_0x370c (8 directions), so it is now countof(DAT_TerrainDefinedData::instance.field_0x370c) (6f13203).

this->GfxLayer[this->DAT_SomeTile] = GMFirstPictureIndex::instance[5] + 668
+ this->LuminesenceLayer[this->DAT_SomeTile] * 8
+ (this->RandomLayer[this->DAT_SomeTile] & 7);
for (local_14_f = 0; local_14_f < 7; local_14_f++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the same padded table: now countof(TerrainFlagGraphicLookup) - 1 with the comment (6f13203).

this->GfxLayer[this->DAT_SomeTile] = 0;
continue;
} else {
goto plainTerrain;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you let the AI check the whole branches and conditions?
There are multiple gotos, but it could always be that certain checks are skippend, or and additional flag was used and/or (if not optimized maybe the only variant), that the scopes and branches are wrongly set and the cases can actually fall through to the cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Beyond reading the code, it is checked by the match: updateGfxLayer is 100.0% raw in a full build with both resolver sets on (main merged, 6f13203). Every compare, branch target and shared tail is therefore identical to 0x509180, so no check is skipped and no case falls through differently. The flag-based and goto-free forms I tried are listed in the PR comment (best: 77.4%).

Comment thread status/addresses-SHC-3BB0A8C1.txt Outdated
SHC_3BB0A8C1_0x00508EC0 | 0.0% | Pending

SHC_3BB0A8C1_0x00509180 | 0.0% | Pending
SHC_3BB0A8C1_0x00509180 | 100.0% | Reimplemented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No volatile marker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added: the remark now says that the GMFirstPictureIndex entries are read through a volatile cast (GM_FIRST_PICTURE_INDEX) for the match (86d2783).

gynt and others added 3 commits December 5, 2026 03:32
updateGfxLayer classifies a wall tile by the corner its two cardinal wall neighbours form: 1 = east+south, 2 = south+west, 3 = north+west, 4 = north+east, 0 = none. It then rotates that value by mapOrientation / 2 to pick the corner alpha graphic.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…d table sizes

directionTranslationMatrix offsets use CardinalDirection (CD_*), with CD_UPPER_LIMIT for the row stride and the direction wrap. The shore switch cases are NeighbourMask combinations, NM_NORTH >> direction builds the neighbour bit, and bankDirection holds the CD direction the bank faces. The TerrainFlagGraphicLookup scans use countof - 1, because its last entry is zero padding. The listing is identical.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

declaration/type change This requires changes to the generated/exported types or definitions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants